﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace Bule.Malbilden.Domain
{
    public class Meal : IEatable
    {
        private IList<IEaten> food;

        public Meal()
        {
            food = new List<IEaten>();
        }

        public Meal(string name, ICollection<IEaten> foodItems, string portionName, double portionCount)
        {
            food = new List<IEaten>();

            foreach (var item in foodItems)
                food.Add(item);
            
            PortionName = portionName;
            PortionCount = portionCount;
            Name = name;
        }

        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual double PortionCount { get; set; }
        public virtual string PortionName { get; set; }

        public virtual ReadOnlyCollection<IEaten> Food
        {
            get { return new ReadOnlyCollection<IEaten>(food); }
        }

        public virtual void AddFood(IEaten eatenFood)
        {
            this.food.Add(eatenFood);
        }

        public virtual void RemoveFood(IEaten eatenFood)
        {
            this.food.Remove(eatenFood);
        }

        /// <summary>
        /// The size of a portion in grams.
        /// </summary>
        public virtual double PortionSize
        {
            get { return TotalWeight / PortionCount; }
            set { PortionCount = TotalWeight / value; }
        }

        private double TotalWeight
        {
            get
            {
                double totalWeight = 0;

                foreach (var item in food)
                    totalWeight += item.Weight;

                return totalWeight;
            }
        }

        /// <summary>
        /// Calculates the total nutritional value of one portion of the meal.
        /// </summary>
        /// <returns>A NutritionInfo instance representing the total nutritional value
        /// of one portion of the meal. Name, PortionName and PortionSize are undefined
        /// for the resulting NutritionInfo object.</returns>
        public virtual NutritionData NutritionalValuePerPortion
        {
            get
            {
                NutritionData totalNutritionValue = new NutritionData();

                foreach (IEaten item in food)
                    totalNutritionValue += item.TotalNutritionalValue;

                return totalNutritionValue / PortionCount;
            }
        }

        public virtual NutritionData NutritionPerHundredGrams
        {
            get
            {
                NutritionData totalNutritionValue = new NutritionData();

                foreach (IEaten item in food)
                    totalNutritionValue += item.TotalNutritionalValue;

                return totalNutritionValue / TotalWeight * 100;
            }
        }
    }
}
